package com.huanwei.cc.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.huanwei.cc.R;
import com.huanwei.cc.newbean.KeCheng;
import com.huanwei.cc.utils.JSONUtils;
import com.huanwei.http.API;
import com.huanwei.http.Okhttp;
import com.huanwei.login_regist.Curriculum_Activity;
import com.huanwei.me_question.adapter.WDWT_Adapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ADM on 2017/12/2.
 * 课程管理
 */

public class CurriculumList_activity extends FragmentActivity {


    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.button2)
    Button button2;
    @BindView(R.id.lv_list)
    ListView lvList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_kecheng);
        ButterKnife.bind(this);
        initViewsAndEvents();
        getCurriculums();
    }

    WDWT_Adapter bank_card_adapter;

    public void initViewsAndEvents() {
        RelativeLayout img_back = (RelativeLayout) findViewById(R.id.img_back);//设置;
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ListView lvList = (ListView) findViewById(R.id.lv_list);//设置;
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                startActivity(new Intent(this, xx.class));
            }
        });

        bank_card_adapter = new WDWT_Adapter();
        lvList.setAdapter(bank_card_adapter);
        lvList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                showDialogs(position);
                return false;
            }
        });


    }

    List<KeCheng.DataBean> ccList = new ArrayList<>();

    /**
     * 这是兼容的AlertDialog
     */
    private void showDialogs(final int Position) {
      /*
       * 这里使用了 android.support.v7.app.AlertDialog.Builder
       * 可以直接在头部写 import android.support.v7.app.AlertDialog
       * 那么下面就可以写成 AlertDialog.Builder
       */
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("确定要删除此课程？");
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                KeCheng.DataBean dataBean = ccList.get(Position);
                API.detelCurriculum("" + dataBean.getId(), new Okhttp.Objectcallback() {
                    @Override
                    public void onsuccess(String st) {
                        Toast.makeText(CurriculumList_activity.this, "删除课程成功", Toast.LENGTH_SHORT).show();
                        getCurriculums();
                    }

                    @Override
                    public void onFalia(int code, String errst) {
                        Toast.makeText(CurriculumList_activity.this, "网络错误", Toast.LENGTH_SHORT).show();
                        Log.e("1", "xx" + errst);
                    }
                });
            }
        });
        builder.show();

    }

    public void getCurriculums() {
        ccList.clear();
        API.getCurriculums(new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Log.e("111", "xx" + st);


                KeCheng teacher = JSONUtils.parseJSON(st, KeCheng.class);
                if (teacher.isIssuccess() == true) {
                    ccList = teacher.getData();
                    if (ccList != null && ccList.size() > 0) {
                        bank_card_adapter.addItem(ccList);
                    } else {
                        Toast.makeText(CurriculumList_activity.this, "数据为空！", Toast.LENGTH_SHORT).show();
                    }

                } else {
                    Toast.makeText(CurriculumList_activity.this, teacher.getMsg(), Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFalia(int code, String errst) {
                Toast.makeText(CurriculumList_activity.this, "网络错误！", Toast.LENGTH_SHORT).show();
            }
        });
    }


    @OnClick({R.id.img_back, R.id.button2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.button2:
                startActivity(new Intent(this, Curriculum_Activity.class));
                break;
        }
    }
}
